package io.unicorn.modules.watersub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;
import io.unicorn.modules.water.dao.ProDataAnalyseDao;
import io.unicorn.modules.water.entity.ProDataAnalyseEntity;
import io.unicorn.modules.watersub.service.ProDataAnalyseSubService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("proDataAnalyseSubService")
public class ProDataAnalyseSubServiceImpl extends ServiceImpl<ProDataAnalyseDao, ProDataAnalyseEntity> implements ProDataAnalyseSubService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProDataAnalyseEntity> page = this.page(
                new Query<ProDataAnalyseEntity>().getPage(params),
                new QueryWrapper<ProDataAnalyseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getDataByProcedure(Map<String, Object> params) {
        Integer pageSize = (Integer.parseInt(params.get("limit").toString()));
        Integer currPage = (Integer.parseInt(params.get("page").toString()));

        List<ProDataAnalyseEntity> dataList = baseMapper.execProDataAnalyse(params);
        Integer total = dataList.size();
        //从第几条数据开始
        Integer firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        Integer lastIndex = currPage * pageSize;

        lastIndex = lastIndex > total ? total : lastIndex;

        //System.out.println(dataList);

        return new PageUtils(dataList.subList(firstIndex, lastIndex),total,pageSize,currPage);
    }

    @Override
    public List<String> queryDataDate(Map<String, Object> params) {
        return baseMapper.selectDataDate(params);
    }

}
